#include "sockethandler.h"

#include "netlib.h"

#include <iostream>
using namespace std;

namespace {

class Warp : public net::ISocketHandler , public luabind::wrap_base {
public:
		virtual void OnRead(net::ISocket* socket) {
            call<void>("OnRead", socket);
        }

		virtual void OnWrite(net::ISocket* socket) {
            call<void>("OnWrite", socket);
        }

		virtual void OnClose(net::ISocket* socket) {
            call<void>("OnClose", socket);
        }

		virtual void OnConnected(net::ISocket* socket) {
            call<void>("OnConnected", socket);
        }

		virtual void OnError(net::ISocket* socket, uint32_t error) {
            call<void>("OnError", socket);
        }
};

}

namespace kxlua {

SocketHandler::SocketHandler(NetLib* lib) {
    lib_ = lib;
    name_ = "SocketHandler";
    isOpen_ = false;
}

SocketHandler::~SocketHandler() {
}

const std::string& SocketHandler::GetName() const {
    return name_;
}

bool SocketHandler::Open() {
    if (!isOpen_) {
        luabind::module(lib_->GetLua().GetState()) 
        [
            luabind::class_<net::ISocketHandler, Warp>("SocketHandler")
            .def(luabind::constructor<>())
            .def("OnRead", &net::ISocketHandler::OnRead, &Warp::OnRead)
            .def("OnWrite", &net::ISocketHandler::OnWrite, &Warp::OnWrite)
            .def("OnClose", &net::ISocketHandler::OnClose, &Warp::OnClose)
            .def("OnConnected", &net::ISocketHandler::OnConnected, &Warp::OnConnected)
            .def("OnError", &net::ISocketHandler::OnError, &Warp::OnError)
        ];
        isOpen_ = true;
    }
    return true;
}

bool SocketHandler::Close() {
}

}
